﻿
<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>Bom更新</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <script src="~/js/framework/sysframework.js"></script>
    <link href="~/js/layui/css/layui.css" rel="stylesheet" />
    <link href="~/X-admin/css/font.css" rel="stylesheet" />
    @* <script src="~/X-admin/lib/layui/layui.js"></script> *@
    <script src="~/js/layui/layui.js"></script>
    <link href="~/X-admin/css/xadmin.css" rel="stylesheet" />
    <script src="~/X-admin/js/jquery.min.js"></script>
    <script src="~/X-admin/js/xadmin.js"></script>
    <script src="~/js/framework/commonjs.js"></script>
    <link href="~/css/mytable.css" rel="stylesheet" />
</head>
<body>
    <ul lay-bar="disabled" class="mWebBanner">
        <li class="layui-nav-item" style="float:left;">
            <button id="btn-update" type="button" name="btn_update" onclick="saveData()" class="buttonCss">
                <i class="layui-icon" style="margin-right:3px">&#x1005;</i>保存BOM
            </button>
        </li>


        <li class="layui-nav-item" style="float:left;margin-left:3%">
            <button id="btn-sync" type="button" onclick="closeWin()" class="buttonCss">
                <i class="layui-icon" style="margin-right:3px">&#x1007;</i> 关闭页面
            </button>
        </li>
    </ul>
    <div style="height:800px;overflow-y: scroll;">
    <div class="layui-card" style="margin-top:10px">
        <div class="layui-card-body" style="padding-top:4%">
            <form id="dForm" class="layui-form layui-col-space5" lay-filter="form-filter">
                <div class="layui-form-item">
                    <div class="layui-input-group" style="float:left;">
                        <div class="layui-input-prefix">
                            BOM名称
                        </div>
                        <input type="text" id="bomName" name="bomName" class="layui-input" style="width:200%">
                    </div>
                    <div class="layui-input-group" style="float:left;margin-left:30%">
                        <div class="layui-input-prefix">
                            BOM编码
                        </div>
                        <input type="text" id="bomCode" name="bomCode" class="layui-input" style="width:120%" readonly />
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-group" style="float:left;">
                        <div class="layui-input-prefix">
                            生产存货
                        </div>
                        <input type="text" id="proName" name="proName" class="layui-input" style="width:200%">
                        <div class="layui-input-suffix" style="cursor: pointer;padding-left:57%" onclick="chooseProduct(1)">
                            <i class="layui-icon layui-icon-more-vertical"></i>
                        </div>
                    </div>
                    <div class="layui-input-group" style="float:left;margin-left:28%">
                        <div class="layui-input-prefix">
                            存货规格
                        </div>
                        <input type="text" id="spec" name="spec" class="layui-input" style="width:120%" readonly />
                    </div>
                </div>
               
                <div class="layui-form-item">
                    <div class="layui-input-group" style="float:left;">
                        <div class="layui-input-prefix">
                            备注说明
                        </div>
                        <input type="text" id="memo" name="memo" class="layui-input" style="width:300%">
                        
                    </div>
                 
                </div>
                <input type="hidden" name="id" id="id" />
                <input type="hidden" name="relationCode" id="relationCode" />
            </form>
                <div class="layui-form-item">
                  
                    <p>
                    <button class="layui-btn" id="selectData" type="button" onclick="chooseProduct(2)"><i class="layui-icon"></i>选择原辅料</button>
                    </p>
                 
                <div class="layui-card-body layui-table-body layui-table-main" id="layui-table-main">
                    <table class="layui-hide" id="list" lay-filter="tableEvent"></table>
                </div>
                </div>
            <div class="layui-form-item">

                <p>
                    <button class="layui-btn" id="selectData" type="button" onclick="chooseBanGun()"><i class="layui-icon"></i>选择版辊</button>
                </p>

                <div class="layui-card-body layui-table-body layui-table-main" id="layui-table-main">
                    <table class="layui-hide" id="bglist" lay-filter="bglist"></table>
                </div>
            </div>
            
        </div>
     
    </div>
    </div>
</body>
</html>
<script src="~/js/tableedit.js"></script>
<script type="text/html" id="zyTools">
    <a title="删除信息" href="javascript:;" lay-event="del" ><i class="layui-icon">&#xe640;</i></a>
</script>
<script>
  
    var eventCode = "0";
    var bomCode = "";
    $(function () {
        bomCode = getUrlVariable("bomCode");
        selectData();
        if (bomCode != "" && bomCode != "0") {
            loadBomInfo();
            loadMeterial();
            loadBanGun();
        }
    });
    /**
     * 原辅料数据列表
     * **/
    function loadMeterial() {
    
        layui.config().extend({
            //tableEdit: '../Content/layui/tableEdit.js'
        }).use(['table', 'tableEdit', 'layer'], function () {
            var table = layui.table, tableEdit = layui.tableEdit, $ = layui.$;
            var cols = table.render({
                elem: '#list'
                , id: 'id'
                , url: '../Basic/GetDataList?condition=' + bomCode + '&dataType=bmm'
                //, height: $(document).height() - $('#list').offset().top - 20
                , page: true
                , limits: [50, 70, 100]
                , limit: 50 //每页默认显示的数量
                , cols: [[
                    { field: 'materialCode', align: 'center', title: '类别编码', width: 130 }
                    , { field: 'proName', align: 'center', title: '类别名称', width: 180 }
                    , { field: 'memo', align: 'center', title: '使用规格', width: 180, edit: 'text' }
                    , { field: 'standardNum', align: 'center', title: '标准基数', width: 110, edit: 'text' }
                    , { field: 'number', align: 'center', title: '标准用量', width: 120, edit: 'text' }
                    ,{
                        field: 'unitName', align: 'center', title: '计量单位', width: 120
                        , event: 'chanageUnit', config: { type: 'select', data: params, verify: true }
                        , templet: function (d) {
             
                            if (d.unitName) {

                                if (d.unitName.value != undefined) {
                                    d.code = d.unitName.value
                                    return d.unitName.name;
                                } else {
                                    return d.unitName;
                                }
                            }
                            return ''
                        }
                    }

                    , { field: 'right', title: '操作', width: 100, align: 'center', toolbar: '#zyTools' }
                ]], done: function (res, curr, count) {
                    currPage = curr;
                    //TablePowerHide();
                }
            }).config.cols;
            var aopTable = tableEdit.aopObj(cols); //获取一个aop对象
            aopTable.on('tool(tableEvent)', function (obj) {
                
                var field = obj.field; //单元格字段
                var value = obj.value; //修改后的值
                var data = obj.data; //当前行旧数据
                var event = obj.event; //当前单元格事件属性值
                var update = {};
                update[field] = value;
                obj.update(update);
                if (event == 'chanageUnit') {
                    EditMaterial(data.id, "unitCode", obj.value.value);
                } else if (event == "del") {
                    layer.confirm('确定要删除选中数据？', function (index) {
                        var rsl = DelMsg(data.id);
                        if (rsl.result) {
                            obj.del();
                            layer.msg("删除成功");
                        } else {
                            layer.msg(info.message);
                        }
                        layer.close(index);

                    });
                }
             
            });


            //监听单元格编辑
            table.on('edit(tableEvent)', function (obj) {
                var data = obj.data;
                EditMaterial(data.id, obj.field, obj.value);

            });
            /**
             * 级联下拉框 => 点击事件生成下拉框之前的回调函数
             * 主要用于 => 动态获取单元格下拉数据
             * tableEvent => table的lay-filter属性值
             * 如示例代码所示
             */
            tableEdit.on('clickBefore(tableEvent)', function (obj) {
                var cascadeSelectData = obj.data; //级联数据
                var cascadeSelectField = obj.field; //级联字段
                if (Array.isArray(cascadeSelectData)) {
                    cascadeSelectData = cascadeSelectData[0];
                }
                //这里用定时器来模拟异步操作，同步操作直接return即可。
                setTimeout(function () {
                    if (cascadeSelectField === 'rqjl') {
                        tableEdit.callbackFn("async(tableEvent)", result1);
                    } else {
                        tableEdit.callbackFn("async(tableEvent)", result);
                    }
                }, 500);
            });
        });
        selectData();
    }
    function chooseProduct(eCode) {
        // console.log(eCode)
        var single = "1";
        let url = "../Basic/SelectInventory?isSingle=" + single;
        if (eCode == 2) { 
            single = "0";
            if (bomCode == "") {
                layer.msg("未找到BOM信息，请先保存BOM信息"); return;
            }
            url = "../Basic/SelectProType?isSingle=" + single;
        }
     // console.log(url)
        eventCode = eCode;
        InfoOpenPage(url, "存货选择", "95%", "90%");

    }
    function chooseBanGun() {
        if (bomCode == "") {
            layer.msg("未找到BOM信息，请先保存BOM信息"); return;
        }
        var proCode = $("input[ name='relationCode']").val();
        let url = "../Basic/SelectBanGun?proCode=" + proCode+"&bomCode="+bomCode;
        InfoOpenPage(url, "版辊选择", "95%", "90%");

    }
  
    var params = undefined;//工序大类
    /**
    获取计量单位下来
    **/
    function selectData() {
        var r = undefined;
        $.ajax({
            url: "../Basic/getDictionaryInfo",
            data: { type: "prt" },
            dataType: "json",
            type: "POST",
            async: false,
            success: function (data) {
                if (data.result) {
                    params = data.data;
                    r = data.data;
                }
            }
        });
        return r;
    }

   /**
    * 删除信息
    * **/
    function DelMsg(dataId) {
        var info = undefined;
        $.ajax({
            url: "../Basic/DelBomSubDataById",
            data: { dataId: dataId },
            dataType: "json",
            type: "POST",
            async: false,
            success: function (rsl) {
                info = rsl;
            }
        });
        return info
    }
    function EditMaterial(dataId,fields, value) {
 
        $.ajax({
            url: "../Basic/EditBomSunMaterial",
            data: { dataId: dataId, fieldName: fields, fieldValue: value },
            dataType: "json",
            type: "POST",
            success: function (data) {
                if (data.result) {
                    layer.msg("保存成功");
                } else {
                    layer.msg(data.message);
                }
            }
        });
    }
    /*
    *加载版辊
    * */
    function loadBanGun() {
        layui.use('table', function () {
            var table = layui.table;

            table.render({
                elem: '#bglist'
                , url: '../Basic/GetDataList?condition=' + bomCode + '&dataType=bmb'
                , skin: 'row' //表格风格
                , even: true
                , page: true
                , limits: [20, 30, 40]
                , limit: 20 //每页默认显示的数量
                , cols: [[
                    { field: 'materialCode', align: 'center', title: '版辊编码', width: 130 }
                    , { field: 'byCode', align: 'center', title: '版 号', width: 110 }
                    , { field: 'banchang', align: 'center', title: '版 长', width: 90 }
                    , { field: 'banzhou', align: 'center', title: '版 周', width: 110}
                    , { field: 'supName', align: 'center', title: '供方名称', width: 200 }
                    , { field: 'right', align: 'center', title: '', toolbar: '#zyTools', width: 90 }
                ]], done: function (data) {

                }
            });
            //监听工具条
            table.on('tool(bglist)', function (obj) {
                var data = obj.data;
                if (obj.event == 'del') {
                    layer.confirm('确定要删除选中数据？', function (index) {
                        var rsl = DelMsg(data.id);
                        if (rsl.result) {
                            obj.del();
                            layer.msg("删除成功");
                        } else {
                            layer.msg(info.message);
                        }
                        layer.close(index);

                    });
                }
            });
        });
    }
    function getChildPar(pars) {
        if (eventCode == "1") {
            if (pars != undefined) {
                $("input[ name='proName']").val(pars[0].proName);
                $("input[ name='relationCode']").val(pars[0].proCode);
                $("input[ name='spec']").val(pars[0].spec);
                $("input[ name='bomName']").val(pars[0].proName + "-BOM");
            }
        } else if (eventCode == "2") {
            if (pars != undefined) {          
                AddSubData(pars.proCode, "meterial","m");
            }
        }
    }
    function GetBanGun(pars) {
        if (pars != undefined) {
            var proCodes = "";
            for (var i = 0; i < pars.length; i++) {
                proCodes += pars[i].byBarCode + ",";
            }
            proCodes = proCodes.slice(0, -1);
            AddSubData(proCodes, "bangun","b");
        }
    }
    

    /**
     * bom子表数据添加
     * **/
    function AddSubData(proCodes,dType,tType) {
        $.ajax({
            url: "../Basic/AddBomSubDatas",
            data: { proCodes: proCodes, bomCode: bomCode, dType: dType },
            dataType: "json",
            type: "POST",
            success: function (data) {
                if (data.result) {
                    if (tType == "m") {
                        loadMeterial();
                    } else if (tType == "b") {
                        loadBanGun();
                    }
                } else {
                    layer.msg(data.message);
                }
            }
        });
    }
    /**
     * BOM主表信息保存
     * **/
    function saveData() {
        $.ajax({
            url: "../Basic/UpdateBomData",
            data: { datas: JSON.stringify(FormToJson("dForm")) },
            dataType: "json",
            type: "POST",
            success: function (data) {
                if (data.result) {
                    bomCode = data.data;
                    layer.msg("数据保存成功");
                    var dataId= $("input[ name='id']").val();
                    if (dataId == "" || dataId=="0") {
                        loadBomInfo();
                    }
               
                } else {
                    layer.msg(data.message);
                }
            }
        });
    }
    
    /**
     * 加载bom信息
     ***/
    function loadBomInfo() {
        $.ajax({
            url: "../Basic/GetBomMainByCode",
            data: { bomCode: bomCode },
            dataType: "json",
            type: "POST",
            success: function (data) {
                if (data.result) {
                    layui.use('form', function () {
                        var form = layui.form;
                        form.val('form-filter', data.data[0]);
                        form.render();
                    });
                   
                } else {
                    layer.msg(data.message);
                }
            }
        });
    }
 
</script>

